from myelin.Ns import MYELINS
from myelin.server.core.ft import ModelDriver

from nodetypes.formats.mml.Events import Form
from nodetypes.models.Theme import Theme
from nodetypes.Ns import GRAPHS_NS


class Form(Form):
    
    newThemeModel = Theme
    newModelDriver = staticmethod(ModelDriver.FromRepo)
    
    def createInstance(self, context, node):
        args = context.request.args
        
        model = context.model
        preferences = context.preferences
        
        locale = preferences['locale']
        theme = preferences['theme']
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        #nxa(
        #    nxc(
        #        u'a:title', MYELINS,
        #        content = model.rdf_join('dcTitle')
        #    )
        #)
                
        self.createLocaleOptions(
            context,
            nxa(
                nxc(
                    u'a:xlocale', MYELINS,
                    attributes = {
                        u'selected'     : locale,
                    }
                )
            ),
            locale
        )
        
        self.createThemeOptions(
            context,
            nxa(
                nxc(
                    u'a:xtheme', MYELINS,
                    attributes = {
                        u'selected'     : theme,
                    }
                )
            ),
            locale,
            theme
        )
        
        return node
    
    
    def createLocaleOptions(self, context, node, locale):
        #node.selected = locale
        
        opts = node.xml_append(
            node.xml_create_element(
                u'a:options', MYELINS
            )
        )
        
        oxa = opts.xml_append
        oxc = opts.xml_create_element
        
        for l in context.i18nResources.keys():
            if l == locale:
                oxa(
                    oxc(
                        u'a:option', MYELINS,
                        attributes = {
                            u'key'  : l,
                            u'selected' : u'1',
                        }
                    )
                )
            else:
                oxa(
                    oxc(
                        u'a:option', MYELINS,
                        attributes = {
                            u'key'  : l,
                        }
                    )
                )
            
        return node
    
    
    def createThemeOptions(self, context, node, locale, theme):
        themes = context.i18nResources[locale].themes.items()
        
        opts = node.xml_append(
            node.xml_create_element(
                u'a:options', MYELINS,
                attributes = {
                    u'count' : unicode(len(themes)),
                }
            )
        )
        
        oxa = opts.xml_append
        oxc = opts.xml_create_element
        map = context.uriMapper.apply
        
        newTheme = self.newThemeModel
        dbDriver = newTheme.Initialize(
            self.newModelDriver(
                context.session.repo,
                context.server.graphs[GRAPHS_NS + 'app']['i18n']
            ),
            uriScopeResolver = context.uriScopeResolver
        )        
        newTheme = newTheme.Retrieve
        
        for k,v in themes:
            if k == theme:
                k = oxa(
                    oxc(
                        u'a:option', MYELINS,
                        attributes = {
                            u'key'  : k,
                            u'selected' : u'1',
                        }
                    )
                )
            else:
                k = oxa(
                    oxc(
                        u'a:option', MYELINS,
                        attributes = {
                            u'key'  : k,
                        }
                    )
                )
            
            n = newTheme(dbDriver, v)
            n_join = n.rdf_join
            
            k.xml_append(
                k.xml_create_element(
                    u'a:theme', MYELINS,
                    attributes = {
                        u'node-id'  : v,
                        u'title' : n_join('dcTitle'),
                        u'description' : n_join('dcDescription'),
                    }
                )
            )
            
        return node